package com.richsoft.rsbrowser.wo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends Activity {
    
    private static final int PIC_WIDTH = 1360;
    
    private Handler mHandler = new Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.main);
        WebView webview = (WebView)findViewById(R.id.webview);
        WebSettings settings = webview.getSettings(); 
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);
        webview.setInitialScale(getScale());
        webview.setPadding(0, 0, 0, 0);
        //webview.loadUrl("file:///android_asset/html/index.html");
        webview.loadUrl("http://mstore.wo.com.cn");
        
        webview.addJavascriptInterface(new Object() {  
            public void tel() {  
                mHandler.post(new Runnable() {  
                    public void run() {  
                        Uri telUri = Uri.parse("tel:10086");
                        Intent intent = new Intent(Intent.ACTION_DIAL, telUri);
                        startActivity(intent);
                    }  
                });  
            }
            
            public void setting() {
                mHandler.post(new Runnable() {  
                    public void run() {  
                        Intent i = new Intent();
                        ComponentName component = new ComponentName("com.android.settings", "com.android.settings.Settings");
                        i.setComponent(component);
                        i.setAction("android.intent.action.MAIN");
                        startActivity(i);
                    }  
                }); 
            }
            
            public void browse() {
                mHandler.post(new Runnable() {  
                    public void run() {  
                        Uri uri = Uri.parse("http://www.baidu.com");        
                        Intent it = new Intent(Intent.ACTION_VIEW, uri); 
                        startActivity(it);
                    }  
                }); 
            }
        }, "demo");  
    }
    
    private int getScale(){
        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
        int width = display.getWidth(); 
        Double val = new Double(width)/new Double(PIC_WIDTH);
        val = val * 100d;
        Log.d("rsbrowser", "scale=" + val);
        return val.intValue();
    }
}